package com.sptpc.learnspring.controller;

import cn.hutool.core.util.NumberUtil;
import com.sptpc.learnspring.controller.request.NewDrinkRequest;
import com.sptpc.learnspring.model.Category;
import com.sptpc.learnspring.model.Drink;
import com.sptpc.learnspring.service.CategoryService;
import com.sptpc.learnspring.service.DrinkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
@RequestMapping("/drink")
public class DrinkController {

    @Autowired
    private DrinkService drinkService;
    @Autowired
    private CategoryService categoryService;

    // /drink/?category=2
    @GetMapping("/")
    public ModelAndView getDrinks(@RequestParam("category") Long categoryId) {
        ModelAndView mv = new ModelAndView("category/drinks");
        List<Drink> drinks = drinkService.getAllDrinks(categoryId);
        Category category = categoryService.findOne(categoryId);
        mv.addObject("drinks",drinks);
        mv.addObject("category",category);
        return mv;
    }

//    @PostMapping("/save")
//    public Drink createNewDrink(@Valid NewDrinkRequest drinkRequest, BindingResult result){
//        if (result.hasErrors()){
//            log.warn(result.toString());
//            return null;
//        }
//        return drinkService.saveDrink(drinkRequest);
//    }
//
//    @PostMapping(value = "/save", consumes = MediaType.APPLICATION_JSON_VALUE)
//    public Drink createNewDrinkWithJson(@RequestBody NewDrinkRequest drinkRequest){
//
//        if (drinkRequest.getCategoryId() != null){
//            return drinkService.saveDrink(drinkRequest);
//        }
//        return null;
//    }
//
//    @PostMapping(value = "/save", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    public List<Drink> batchCreateDrink(@RequestParam("file")MultipartFile file){
//        List<NewDrinkRequest> drinkRequests = new ArrayList<>();
//        if (!file.isEmpty()){
//            BufferedReader reader = null;
//            try {
//                reader = new BufferedReader(new InputStreamReader(file.getInputStream()));
//                String line;
//                while ((line = reader.readLine()) != null){
//                    String[]arr = line.split(",");
//                    NewDrinkRequest drinkRequest = new NewDrinkRequest(arr[0],
//                            Long.parseLong(arr[2]),
//                            Money.of(CurrencyUnit.of("CNY"), NumberUtil.toBigDecimal(arr[1])));
//                    drinkRequests.add(drinkRequest);
//                }
//
//                return drinkService.batchSave(drinkRequests);
//
//            } catch (IOException e) {
//                e.printStackTrace();
//            }finally {
//                IOUtils.closeQuietly(reader);
//            }
//        }
//        return null;
//    }

//    @PostMapping(value = "/image",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    public String batchCreateDrink(@RequestParam("file")MultipartFile file,
//                                   @RequestParam("name") String name) {
//        // 对象存储
//        // 1、创建保存文件的file对象
////        file.getOriginalFilename()
//
//        String path = "/Users/colt/Desktop/Java EE Web开发2/image";
//        String filePath = path + File.separator + name;
//        File destFile = new File(filePath);
//        destFile.getParentFile().mkdirs();
//        // 2、保存上传的文件
//        try {
//            file.transferTo(destFile);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return "图片上传成功";
//    }

//    Page<Drink> drinks = drinkService.getAll(PageRequest.of(0, 2, Sort.Direction.DESC, "createTime"));
//    log.info("总记录数:{},\t总页数:{}", drinks.getTotalElements(), drinks.getTotalPages());
//    drinks.getContent().forEach(drink -> log.info("饮品{}", drink));

}
